package library.dao.Impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import library.dao.IlibraryDao;
import library.db.DbConnection;
import library.pojo.Book;
import library.pojo.BookType;
import library.pojo.BorrowHistory;
import library.pojo.User;
import library.pojo.UserType;

public class UserDaoImpl implements IlibraryDao {

	public User login(User user) {
		User resultUser = null ;
		Connection conn = DbConnection.getConnection();
		String sql = "select * from user where username=? and password=?";
		PreparedStatement ps = null;
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, user.getUsername());
			ps.setString(2, user.getPassword());
			ResultSet rs = ps.executeQuery();
			if(rs.next()) {
				String userName = rs.getString(1);
				String passWord = rs.getString(2);
				String name = rs.getString(3);
				String phone = rs.getString(4);
				String type = rs.getString(5);
				resultUser = new User(userName,passWord,name,type,phone);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DbConnection.close(ps, null);
		}
		return resultUser;
	}
	
	public User register(String username) {
		User resultUser = null ;
		Connection conn = DbConnection.getConnection();
		String sql = "select * from user where username=?";
		PreparedStatement ps = null;
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, username);
			ResultSet rs = ps.executeQuery();
			if(rs.next()) {
				String userName = rs.getString(1);
				String passWord = rs.getString(2);
				String name = rs.getString(3);
				String phone = rs.getString(4);
				String type = rs.getString(5);
				resultUser = new User(userName,passWord,name,type,phone);}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DbConnection.close(ps, null);
		}
		return resultUser;
	}
	
	@Override
	public void save(Book book) {
		// TODO Auto-generated method stub

	}

	@Override
	public void update(Book book) {
		// TODO Auto-generated method stub

	}

	@Override
	public List<Book> get(String isbn) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void delete(String isbn) {
		// TODO Auto-generated method stub

	}

	@Override
	public List<Book> findAll() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void addBookType(BookType bookType) {
		// TODO Auto-generated method stub

	}

	@Override
	public void deleteBookType(String idx) {
		// TODO Auto-generated method stub

	}

	@Override
	public void updateBookType(BookType bookType) {
		// TODO Auto-generated method stub

	}

	@Override
	public BookType getBookType(String idx) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<BookType> findAllBookType() {
		// TODO Auto-generated method stub
		return null;
	}

	/**
	 * 增加用户信息
	 */
	@Override
	public void addUser(User user) {
		Connection conn = DbConnection.getConnection();
		String sql = "insert into user values(?,?,?,?,?)";
		PreparedStatement ps = null;
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, user.getUsername());
			ps.setString(2, user.getPassword());
			ps.setString(3, user.getName());
			ps.setString(4, user.getPhone());
			ps.setString(5, user.getType());
			ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DbConnection.close(ps, null);
		}

	}

	/**
	 * 删除用户信息
	 */
	@Override
	public void deleteUser(String username) {
		Connection conn = null;
		PreparedStatement ps = null;
		conn = DbConnection.getConnection();
		String sql = "delete from user where username = ?";
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, username);
			ps.executeUpdate();
		} catch (SQLException e) {
			System.out.println("Jdbc程序发生错误：" + e);
		} finally {
			DbConnection.close(ps, null);
		}

	}

	/**
	 * 修改用户信息
	 */
	@Override
	public void updateUser(User user) {
		Connection conn = DbConnection.getConnection();
		String sql = "update user set password=?,name=?,phone=?,type=? where username=?";
		PreparedStatement ps = null;
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, user.getPassword());
			ps.setString(2, user.getName());
			ps.setString(3, user.getPhone());
			ps.setString(4, user.getType());
			ps.setString(5, user.getUsername());
			ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DbConnection.close(ps, null);
		}

	}

	/**
	 * 查询一个用户信息
	 */
	@Override
	public User getUser(String username) {
		Connection conn = null;
		ResultSet rs = null;
		PreparedStatement ps = null;
		conn = DbConnection.getConnection();
		String sql = "select * from user where username = ?";
		User user = null;
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, username);
			rs = ps.executeQuery();
			while (rs.next()) {
				String userName = rs.getString(1);
				String passWord = rs.getString(2);
				String name = rs.getString(3);
				String phone = rs.getString(4);
				String type = rs.getString(5);
				user = new User(userName, passWord, name,type,phone);
			}
		} catch (SQLException e) {
			System.out.println("Jdbc程序发生错误：" + e);
		} finally {
			DbConnection.close(ps, rs);
		}
		return user;
	}

	
	
	/**
	 * 查询用户姓名
	 */
	public String getUserName(String username) {
		Connection conn = null;
		ResultSet rs = null;
		PreparedStatement ps = null;
		conn = DbConnection.getConnection();
		String sql = "select * from user where username = ?";
		String name = null;
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, username);
			rs = ps.executeQuery();
			while (rs.next()) {
				name = rs.getString(3);
			}
		} catch (SQLException e) {
			System.out.println("Jdbc程序发生错误：" + e);
		} finally {
			DbConnection.close(ps, rs);
		}
		return name;
	}
	
	/**
	 * 查询电话
	 */
	public String getPhone(String username) {
		Connection conn = null;
		ResultSet rs = null;
		PreparedStatement ps = null;
		conn = DbConnection.getConnection();
		String sql = "select * from user where username = ?";
		String phone = null;
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, username);
			rs = ps.executeQuery();
			while (rs.next()) {
				phone = rs.getString(4);
			}
		} catch (SQLException e) {
			System.out.println("Jdbc程序发生错误：" + e);
		} finally {
			DbConnection.close(ps, rs);
		}
		return phone;
	}
	
	/**
	 * 查询用户类型
	 */
	public String getType(String username) {
		Connection conn = null;
		ResultSet rs = null;
		PreparedStatement ps = null;
		conn = DbConnection.getConnection();
		String sql = "select * from user where username = ?";
		String type = null;
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, username);
			rs = ps.executeQuery();
			while (rs.next()) {
				type = rs.getString(5);
			}
		} catch (SQLException e) {
			System.out.println("Jdbc程序发生错误：" + e);
		} finally {
			DbConnection.close(ps, rs);
		}
		return type;
	}
	
	public static void main(String[] args) {
		UserDaoImpl u = new UserDaoImpl();
		System.out.println(u.getUserName("java"));
	}
	/**
	 * 查询所有用户信息
	 */
	@Override
	public List<User> findAllUser() {
		Connection conn = null;
		ResultSet rs = null;
		PreparedStatement ps = null;
		conn = DbConnection.getConnection();
		String sql = "select * from user";
		User user = null;
		List<User> userList = new ArrayList<User>();
		try {
			ps = conn.prepareStatement(sql);
			rs = ps.executeQuery();
			while (rs.next()) {
				String username = rs.getString(1);
				String password = rs.getString(2);
				String name = rs.getString(3);
				String phone = rs.getString(4);
				String type = rs.getString(5);
				user = new User(username, password, name,type,phone);
				userList.add(user);
			}
		} catch (SQLException e) {
			System.out.println("Jdbc程序发生错误：" + e);
		} finally {
			DbConnection.close(ps, rs);
		}
		return userList;
	}

	@Override
	public void addUserType(UserType userType) {
		// TODO Auto-generated method stub

	}

	@Override
	public void deleteUserType(String id) {
		// TODO Auto-generated method stub

	}

	@Override
	public void updateUserType(UserType userType) {
		// TODO Auto-generated method stub

	}

	@Override
	public UserType getUserType(String id) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<UserType> findAllUserType() {
		// TODO Auto-generated method stub
		return null;
	}
	
	public String getPassword(String username) {
		Connection conn = null;
		ResultSet rs = null;
		PreparedStatement ps = null;
		conn = DbConnection.getConnection();
		String sql = "select * from user where username = ?";
		String password = null;
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, username);
			rs = ps.executeQuery();
			while (rs.next()) {
				password = rs.getString(2);
			}
		} catch (SQLException e) {
			System.out.println("Jdbc程序发生错误：" + e);
		} finally {
			DbConnection.close(ps, rs);
		}
		return password;
	}

	@Override
	public void addBorrowHistory(BorrowHistory borrowHistory) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void deleteBorrowHistory(String isbn , String borrowUser) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void updateBorrowHistory(BorrowHistory borrowHistory) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public BorrowHistory getBorrowHistory(String userId) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<BorrowHistory> findAllBorrowHistory() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String getTypeName(String idx) {
		// TODO Auto-generated method stub
		return null;
	}

}
